/**
 *
 */
package org.fixies.spreadsheet.converter;

import org.fixies.spreadsheet.Unit;
import org.fixies.utils.NullValueAvoidingHashMap;

import java.util.Map;

/**
 * @author Kawasaki
 * @version 1.0
 */
public abstract class SheetToMapConverter<T> extends AbstractSheetConverter<Map<String, T>> {

    /* (non-Javadoc)
    * @see org.fixies.spreadsheet.converter.Converter#convert()
    */

    @Override
    public abstract Map<String, T> convert();

    protected final Map<String, T> doConvert() {
        Map<String, T> map = new NullValueAvoidingHashMap<String, T>();
        for (Unit unit : getSheet().getUnits()) {
            if (isTarget(unit)) {
                String key = unit.getId();
                T value = convert(unit);
                map.put(key, value);
            }
        }
        return map;
    }


    protected abstract boolean isTarget(Unit unit);


    protected abstract T convert(Unit unit);

}
